package com.css.upms.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.css.common.core.utils.R;
import com.css.upms.api.entity.SysArea;
import com.css.upms.biz.service.SysAreaService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 区域管理
 *
 * @author hanyx
 * @date 2019-06-06 15:01:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/area")
@Api(value = "/area", tags = {"UPMS-区域管理"})
@Slf4j
public class AreaController {

    private final SysAreaService sysAreaService;

    /**
     * 简单分页查询
     *
     * @param page    分页对象
     * @param sysArea 区域管理
     * @return
     */
    @ApiOperation(value = "简单分页查询")
    @GetMapping("/page")
    public R<IPage<SysArea>> getSysAreaPage(Page<SysArea> page, SysArea sysArea) {
        return new R<>(sysAreaService.getSysAreaPage(page, sysArea));
    }


    /**
     * 通过id查询单条记录
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询单条记录")
    @GetMapping("/{id}")
    public R<SysArea> getById(@PathVariable("id") Integer id) {
        return new R<>(sysAreaService.getById(id));
    }

    /**
     * 新增记录
     *
     * @param sysArea
     * @return R
     */
    @ApiOperation(value = "新增区域管理")
    @PostMapping
//  @PreAuthorize("hasAuthority('biz_sysarea_add')")
    public R save(@RequestBody SysArea sysArea) {
        return new R<>(sysAreaService.save(sysArea));
    }

    /**
     * 修改记录
     *
     * @param sysArea
     * @return R
     */
    @ApiOperation(value = "修改区域管理")
    @PutMapping
//  @PreAuthorize("hasAuthority('biz_sysarea_edit')")
    public R update(@RequestBody SysArea sysArea) {
        return new R<>(sysAreaService.updateById(sysArea));
    }

    /**
     * 通过id删除一条记录
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "删除区域管理")
    @DeleteMapping("/{id}")
//  @PreAuthorize("@hasAuthority('biz_sysarea_del')")
    public R removeById(@PathVariable Integer id) {
        return new R<>(sysAreaService.removeById(id));
    }


    /**
     * 返回树形区域集合
     *
     * @return 树形区域
     */
    @ApiOperation(value = "返回树形区域集合")
    @GetMapping(value = "/tree")
    public R listDeptTrees() {
        return new R<>(sysAreaService.listAreaTrees());
    }

}
